/*
 * Copyright (C) 2012 Andrea Parodi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package container;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
 * User: andrea.parodi
 * Date: 27/04/12
 * Time: 12.04
 */
public final class ValidationResults {

    public ImmutableList<ValidationMessage> getMessages() {
        return messages;
    }

    private final ImmutableList<ValidationMessage> messages;

    private ValidationResults(List<ValidationMessage> results) {
        Preconditions.checkNotNull(results);
        this.messages = ImmutableList.copyOf(results);
    }


    public static ValidationResults from(List<ValidationMessage> results) {
        return new ValidationResults(results);
    }

    @Override public String toString() {
        if (messages.isEmpty()) return "Valid.";

        StringBuilder builder = new StringBuilder();
        builder.append("Not valid:");
        builder.append("\r\n");
        for (ValidationMessage msg : messages) {
            builder.append(msg.toString());
            builder.append("\r\n");
        }

        return builder.toString();
    }

    public ValidationMessage get(int idx) {
        return messages.get(idx);
    }
}
